<template>
  <div class="employees">
    <el-card class="top-card">
      <div class="btn">
        <el-button
          v-bottonShow:inlineBlock="'employees_import'"
          type="primary"
          @click="OutClick"
        >导出</el-button>
        <el-button type="primary" @click="importClick">导入</el-button>
        <el-button type="primary" @click="add">+新增员工</el-button>
      </div>
    </el-card>
    <el-card class="bottom-card">
      <el-table :data="list" style="width: 100%">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            {{ (page.page - 1) * page.size + $index + 1 }}</template>
        </el-table-column>
        <el-table-column label="头像" sortable prop="username">
          <template v-slot="{ row }">
            <img
              v-imgError="defImg"
              class="userPHP"
              :src="row.staffPhoto"
              alt=""
              @click="twoWM(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="姓名" sortable prop="username" />
        <el-table-column label="手机号" sortable prop="mobile" />
        <el-table-column label="工号" sortable prop="workNumber" />
        <el-table-column label="聘用形式" sortable prop="formOfEmployment">
          <template v-slot="{ row }">
            <div>{{ row.formOfEmployment | formatterFormOf }}</div>
          </template>
        </el-table-column>
        <el-table-column label="部门" sortable prop="departmentName" />
        <el-table-column label="入职时间" sortable prop="timeOfEntry" />

        <el-table-column label="状态" sortable prop="enableState">
          <template v-slot="{ row }">
            {{ row.enableState | getStatus }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" sortable fixed="right">
          <template v-slot="{ row }">
            <div class="page">
              <el-button type="text" @click="look(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="lookrole(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          layout="total,sizes,prev,pager,next,jumper"
          :total="page.total"
          :page-sizes="[1, 5, 10, 20, 30, 40]"
          :page-size="page.size"
          :current-page="page.page"
          @size-change="sizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!-- 新增员宫弹框 -->
    <Add ref="add" @getData="getData" />
    <!-- 弹出二维码  -->
    <el-dialog title="扫码" :visible.sync="isSHOW">
      <div style="text-align: center"><canvas ref="canvas" /></div>
    </el-dialog>
    <!-- 弹出的分配角色 -->
    <Role ref="role" :open-role.sync="openRole" />
  </div>
</template>

<script>
import moment from 'moment'
import Qrcode from 'qrcode'
import { sysUserAPIGET, sysuserdeleteAPI } from '@/api'
import cookiejs from 'js-cookie'

export default {
  components: {
    Add: () => import('./components/add.vue'),
    Role: () => import('./components/role.vue')
  },
  data() {
    return {
      list: [{}],
      page: {
        total: 100,
        size: +cookiejs.get('size') || 5,
        page: 1
      },
      defImg: require('@/assets/common/head.jpg'),
      isSHOW: false,
      openRole: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 二维码打出框
    twoWM(staffPhoto) {
      this.isSHOW = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.canvas, staffPhoto, {
          width: 300,
          height: 300
        })
      })
    },
    // 点击导入
    importClick() {
      this.$router.push('/employees/import')
    },
    // 删除员工
    del(row) {
      this.$confirm(`您确定删除此员工吗`, '提示').then(async() => {
        await sysuserdeleteAPI(row.id)
        this.$message.success('删除成功')
        this.pageChange(1)
      })
    },
    // 页容量
    sizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
      cookiejs.set('size', size)
    },
    // 页码
    pageChange(page) {
      this.page.page = page
      this.getData()
    },
    // 获取员工列表
    async getData() {
      const res = await sysUserAPIGET(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      // 循环遍历把每个时间戳 转换时间
      res.data.rows.forEach((v) => {
        v.timeOfEntry = moment(v.timeOfEntry).format('YYYY-MM-DD')
      })
    },
    add() {
      this.$refs.add.isShow = true
    },
    // 跳转到 查看员工
    look(id) {
      this.$router.push('/employees/edit/' + id)
    },
    // 导出
    async OutClick() {
      const header = {
        姓名: 'username', //	string	非必须		姓名
        手机号: 'mobile', //	string	非必须		手机号
        聘用形式: 'formOfEmployment', //	number	非必须		聘用形式
        工号: 'workNumber', //	string	非必须		工号
        组织名称: 'departmentName', //	string	非必须		组织名称
        入职时间: 'timeOfEntry', //	string	非必须		入职时间
        转正时间: 'correctionTime' //	string	非必须		转正时间
      }
      const res = await sysUserAPIGET({
        page: 1,
        size: 10000
      })
      res.data.rows.forEach((v) => {
        v.timeOfEntry = moment(v.timeOfEntry).format('YYYY-MM-DD')
      })
      const data = res.data.rows.map((item) => {
        return Object.keys(header).map((item2) => {
          // item2 等于 header 里面的每一项中文

          return item[header[item2]]
        })
      })

      import('@/vendor/Export2Excel').then((res) => {
        res.export_json_to_excel({
          header: Object.keys(header),
          data,
          filename: '用户列表'
        })
      })
    },
    // 点击角色按钮
    lookrole(row) {
      this.openRole = true
      this.$refs.role.getUserInfo(row.id)
    }
  }
}
</script>

<style lang="scss">
.employees {
  .bottom-card {
    margin: 10px;
    .userPHP {
      height: 70px;
      width: 70px;
    }
  }
  .btn {
    text-align: right;
  }
  .top-card {
    margin: 10px;
  }
  .page {
    text-align: right;
  }
}
</style>
